package com.campus.campusdelivery.controller;

import com.campus.campusdelivery.constant.MerchantStatus;
import com.campus.campusdelivery.dto.ApiResponse;
import com.campus.campusdelivery.dto.MerchantDto;
import com.campus.campusdelivery.dto.MerchantUpdateRequest;
import com.campus.campusdelivery.service.MerchantService;
import com.campus.campusdelivery.util.UserContextUtil;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/merchants")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class MerchantController {
    
    private final MerchantService merchantService;
    private final UserContextUtil userContextUtil;
    
    @GetMapping("/{id}")
    public ApiResponse<MerchantDto> getMerchantById(@PathVariable Long id) {
        try {
            MerchantDto merchant = merchantService.getMerchantById(id);
            return ApiResponse.success(merchant);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @GetMapping("/my")
    public ApiResponse<MerchantDto> getMyMerchant(Authentication authentication) {
        try {
            Long userId = userContextUtil.getCurrentUserId();
            MerchantDto merchant = merchantService.getMerchantByUserId(userId);
            return ApiResponse.success(merchant);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @GetMapping
    public ApiResponse<Page<MerchantDto>> getAllMerchants(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Long categoryId,
            Pageable pageable) {
        try {
            Page<MerchantDto> merchants = merchantService.getAllMerchants(name, categoryId, pageable);
            return ApiResponse.success(merchants);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @GetMapping("/status/{status}")
    public ApiResponse<Page<MerchantDto>> getMerchantsByStatus(
            @PathVariable MerchantStatus status, Pageable pageable) {
        try {
            Page<MerchantDto> merchants = merchantService.getMerchantsByStatus(status, pageable);
            return ApiResponse.success(merchants);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @GetMapping("/open")
    public ApiResponse<List<MerchantDto>> getOpenMerchants() {
        try {
            List<MerchantDto> merchants = merchantService.getOpenMerchants();
            return ApiResponse.success(merchants);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @PutMapping("/my")
    public ApiResponse<MerchantDto> updateMyMerchant(
            Authentication authentication, @Valid @RequestBody MerchantUpdateRequest request) {
        try {
            Long userId = userContextUtil.getCurrentUserId();
            MerchantDto merchant = merchantService.updateMerchant(userId, request);
            return ApiResponse.success("更新成功", merchant);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @PutMapping("/{id}/status")
    public ApiResponse<Void> updateMerchantStatus(
            @PathVariable Long id,
            @RequestParam MerchantStatus status,
            @RequestParam(required = false) String rejectReason) {
        try {
            merchantService.updateMerchantStatus(id, status, rejectReason);
            return ApiResponse.success("状态更新成功", null);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @PutMapping("/my/toggle-open")
    public ApiResponse<Void> toggleMyMerchantOpenStatus(Authentication authentication) {
        try {
            Long userId = userContextUtil.getCurrentUserId();
            merchantService.toggleMerchantOpenStatus(userId);
            return ApiResponse.success("营业状态切换成功", null);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteMerchant(@PathVariable Long id) {
        try {
            merchantService.deleteMerchant(id);
            return ApiResponse.success("删除成功", null);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
}
